var layer;
var element;
var form;
var laydate;
var table;
var upload;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    upload = layui.upload;
    //日期时间范围
    laydate.render({
        elem: '#registerTime'
        ,type: 'datetime'
        ,trigger: 'click' //时间时间为点击事件
        ,range: true
    });
    load();
    show();

    //全部订单数
    OrdersTotal(2);
    //待付款订单数
    payment(2,2);
    //已付款订单数
    sendGoods(2,3);
    //已取消订单总数
    cancelled(2,10);

    //但是，如果你的HTML是动态生成的，自动渲染就会失效
    //因此你需要在相应的地方，执行下述方法来进行渲染
    form.render();
})

//统计订单数量
//统计全部课程订单的数量
function OrdersTotal(orderType){
    $.post("/SyOperationController/getOrderNumber",{"orderType":orderType},function (data) {
        if(data.data==null){
            data.data=0;
        }else{
            data.data=data.data;
        }
        $("#AllOrders").text(data.data);
    });
}
//统计待付款课程订单的数量
function payment(orderType,orderStatus){
    $.post("/SyOperationController/getOrderNumber",{"orderType":orderType,"orderStatus":orderStatus},function (data) {
        if(data.data==null){
            data.data=0;
        }else{
            data.data=data.data;
        }
        $("#payment").text(data.data);
    });
}
//统计已付款课程订单的数量
function sendGoods(orderType,orderStatus){
    $.post("/SyOperationController/getOrderNumber",{"orderType":orderType,"orderStatus":orderStatus},function (data) {
        if(data.data==null){
            data.data=0;
        }else{
            data.data=data.data;
        }
        $("#sendGoods").text(data.data);
    });
}
//统计已取消课程订单的数量
function cancelled(orderType,orderStatus){
    $.post("/SyOperationController/getOrderNumber",{"orderType":orderType,"orderStatus":orderStatus},function (data) {
        if(data.data==null){
            data.data=0;
        }else{
            data.data=data.data;
        }
        $("#cancelled").text(data.data);
    });
}

/**
 *  按钮  点击事件 刷新页面  返回  检索
 * */
function load() {
    //搜索按钮
    $("#indexBtn").bind('click', function () {
        //支付渠道
        var orderPaymentChannel = $("select[name=orderPaymentChannel]").val();
        //订单状态
        var orderStatus = $("select[name=orderStatus]").val();
        //订单来源
        var orderSource = $("select[name=orderSource]").val();
        selAuditList(orderPaymentChannel, orderStatus, orderSource);
    })
    /*let i = true;
    //收起筛选，放下
    $("#put_screen").bind('click', function () {
        if (i) {//判断i如果为true，则隐藏搜索，如果为false则展示搜索
            $("#selIndex").attr("style", "display:none;");
            $(".div_list_title").attr('style', "margin-top: -84px;")
            $("#put_screen").html("<i class='layui-icon layui-icon-up ayui-font-9'></i>打开筛选")
            i = false;
        } else {
            $("#selIndex").attr("style", "dispaly:block;");
            $(".div_list_title").attr('style', "margin-top: 0px;")
            $("#put_screen").html("<i class='layui-icon layui-icon-down ayui-font-9'></i>收起筛选")
            i = true;
        }
    })*/
}

//刷新页面按钮
$("#refreshs").bind('click', function () {
    location.reload();
})
//重置按钮
$("#resetBtn").bind('click', function () {
    show();
})

/*//删除订单
function delAuditUser(orderId){
    $.post("/SyOperationController/delProductOrderDetails", {orderId:orderId}, function (res) {
        selAuditList();
    });
}*/

//高级检索
function retrieve(){
    var orderStatus=$('input[name=orderStatus1]').val();
    var nicknames=$('input[name=nicknames]').val();
    var orderNo=$('input[name=orderNos]').val();
    var opc=$('input[name=orderPaymentChannels]').val();
    var orderSource=$('input[name=orderSources]').val();
    var courseName=$('input[name=courseNames]').val();

    $.post("/SyOperationController/selCourseOrderListData",
        {"orderPaymentChannel":opc,"orderStatus":orderStatus,"orderSource":orderSource,"nickname":nicknames,"orderNo":orderNo,"courseName":courseName},
        function (res) {
            userTab(res.data);
        }
    );
}
//高级检索弹出层
var search=function(){
    layer.open({
        type:2,  //弹出层类型
        title:"高级检索",
        area:['580px','470px'], //弹出层大小
        shade:0.4, //设置遮罩透明度
        offset:'auto', //设置遮罩位置
        content:'CourseOrderList_search.html', //弹出层内容页面
        success: function (data) {}
    })
    window.layer_1=layer;
}

//查询
function selAuditList(orderPaymentChannel,orderStatus,orderSource){
    $.post("/SyOperationController/selCourseOrderListData", {orderPaymentChannel:orderPaymentChannel,orderStatus:orderStatus,orderSource:orderSource}, function (res) {
        userTab(res.data);
    });
}
//layui-table
function userTab(item){
    table.render({
        elem:"#userListTab",//容器
        title:"课程订单列表",
        data:item,
        toolbar:"#toolbar_id",
        page:true,
        cols:cols
    });
}
//layui-table格式
var cols=[[//定义列
    {type:'checkbox',align:'center',width:40},//复选框靠左
    {title:'序号',align:'center',width:60,templet:function(data){
            return data.LAY_INDEX;
        }},
    {field:"order_no",title:"订单编号",align:'center',width:195},
    {field:"nickname",title:"用户昵称",align:'center',},
    {field:"course_name",title:"课程名称",width:180,align:'center',},
    //{field:"phone",title:"用户手机",align:'center',width:120},
    /*{field:"logistics_fee",title:"订单金额",align:'center',templet:function(data){
            return  " ￥"+data.logistics_fee;
        }},*/
    {field:"order_source",title:"订单来源",align:'center',templet:function(data){
            if(data.order_source==2){//2为小程序
                return "小程序";
            }else if(data.order_source==3){//3为app
                return "app"
            }else{
                return "其他"
            }
        }},
    {field:"order_status",title:"订单状态",align:'center',templet:function(data){
            if(data.order_status==2){//待付款
                return "待付款"
            }else if(data.order_status==3){//已付款
                return "已付款"
            }else if(data.order_status==10){//取消交易
                return "取消支付"
            }else{//其他
                return "其他"
            }
        }},
    //{field:"course_intro",title:"课程描述",align:'center',width:120},
    {field:"order_payment_channel",title:"支付渠道",align:'center',templet:function(data){
            if(data.order_payment_channel==2){//2微信支付
                return "微信支付"
            }else if(data.order_payment_channel==3){//3余额支付
                return "余额支付"
            }else if(data.order_payment_channel==4){//4银行卡支付
                return "未支付"
            }else if(data.order_payment_channel==5){//5支付宝支付
                return "支付宝支付"
            }
        }},{field:"true_name",title:"主讲教师",align:'center',width:120},
    {field:"",title:"操作",align:'center',width:100,templet:function(data){
            return "<a style='color:#009688;cursor:pointer;' onclick='selAuditUser(\""+data.order_id+"\")'>查看</a>";//+
                //"<a style='color:#009688;cursor:pointer;' onclick='delAuditUser(\""+data.order_id+"\")'>删除</a> ";
        }}
]];


//状态按钮
function selAuditLists(orderStatus){
    $.post("/SyOperationController/selCourseOrderListData", {orderStatus:orderStatus}, function (res) {
        userTab(res.data);
    });
}

//展示table表格
function show(){
    $(" #AllOrderss").removeClass("layui-btn-primary");
    $(" #payments").addClass("layui-btn-primary");
    $(" #sendGoodss").addClass("layui-btn-primary");
    $(" #cancelleds").addClass("layui-btn-primary");
    selAuditLists();
}
//全部课程的订单列表
$('#AllOrderss').on('click',function(){
    $(" #AllOrderss").removeClass("layui-btn-primary");
    $(" #payments").addClass("layui-btn-primary");
    $(" #sendGoodss").addClass("layui-btn-primary");
    $(" #cancelleds").addClass("layui-btn-primary");
    selAuditLists();
})
//待付款课程的订单列表
$('#payments').on('click',function(){
    var orderStatus=2;
    $('input[name=orderStatus1]').val(2);
    $(" #AllOrderss").addClass("layui-btn-primary");
    $(" #payments").removeClass("layui-btn-primary");
    $(" #sendGoodss").addClass("layui-btn-primary");
    $(" #cancelleds").addClass("layui-btn-primary");
    selAuditLists(orderStatus);
})
//待发货课程的订单列表
$('#sendGoodss').on('click',function(){
    var orderStatus=3;
    $('input[name=orderStatus1]').val(3);
    $(" #AllOrderss").addClass("layui-btn-primary");
    $(" #payments").addClass("layui-btn-primary");
    $(" #sendGoodss").removeClass("layui-btn-primary");
    $(" #cancelleds").addClass("layui-btn-primary");
    selAuditLists(orderStatus);
})
//已取消课程的订单列表
$('#cancelleds').on('click',function(){
    var orderStatus=10;
    $('input[name=orderStatus1]').val(10);
    $(" #AllOrderss").addClass("layui-btn-primary");
    $(" #payments").addClass("layui-btn-primary");
    $(" #sendGoodss").addClass("layui-btn-primary");
    $(" #cancelleds").removeClass("layui-btn-primary");
    selAuditLists(orderStatus);
})


//查看课程订单详情
var selAuditUser=function(orderId){
    localStorage.setItem("orderId",orderId); //订单ID
    window.location.href='CourseOrderList_layer_sel.html';
    //CourseOrderList_layer_sel.html
}


